package cn.edu.sdjzu.xg.bysj.controller.basic;
//201902104061徐中堂
import cn.edu.sdjzu.xg.bysj.domain.GraduateProjectStatus;
import cn.edu.sdjzu.xg.bysj.service.GraduateProjectStatusService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import util.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
@Slf4j
// 将所有方法组织在一个Controller(Servlet)中，应用过滤器
@WebServlet("/basic/graduateProjectStatus.ctl")
public class GraduateProjectStatusController extends HttpServlet {
    /**
     * GET, http://localhost:8080/basic/graduateProjectStatus.ctl, 查询毕设项目状态
     * 查询毕业设计项目对象：将来自前端请求的JSON对象
     * @param request  请求对象
     * @param response 响应对象
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //创建JSON对象respMessage_jsonObj，以便往前端响应信息
        JSONObject respMessage_jsonObj = new JSONObject();
        //将request body转换为JSON字串
        String req_jsonStr = JSONUtil.getJSON(request);
        //将request body解析为JSONObject对象
        JSONObject req_jsonObj = JSON.parseObject(req_jsonStr);
        //如果请求body为空
        if (req_jsonObj == null) {
            respMessage_jsonObj.put("message","未收到请求信息！");
            response.getWriter().println(respMessage_jsonObj);
            log.error("未收到信息");
            return;
        }
        //从JSONObject对象中读取键“id”的值（Java Object对象），req_jsonObj可能是{"id":1}
        Object id_obj = req_jsonObj.get("id");
        try {
            //如果id_Obj != null, 说明请求数据有id键，是请求某个对象
            if (id_obj != null) {
                int id_int = Integer.parseInt(id_obj.toString());
                responseGraduateProject(id_int, response);
                log.trace("查找id" + id_int);
            } else {//请求中包含条件和分页信息，需要响应多个对象
                //读取分页信息，形成分页对象
                //从JSONObject对象中读取 “pagination” 键对应的值(JSONObject对象)
                Pagination pagination = ControllerHelper.getPagination(req_jsonObj);
                //从请求JSONObject对象中读取键“condition”对应的值(描述查询条件的JSONArray对象)
                List<Condition> conditionList = ControllerHelper.getConditions(req_jsonObj);
                //响应多个信息，其中conditionList, pagination两个对象引用为空是可能的
                this.responseGraduateProjects(response);
            }
        } catch (SQLException e){
            respMessage_jsonObj.put("message","数据库异常");
            log.error("数据库异常"+ e.getMessage());
        } catch(Exception e){
            respMessage_jsonObj.put("message","网络异常");
            log.error("网络异常" + e.getMessage());
        }
        finally {
            response.getWriter().println(respMessage_jsonObj);
        }
    }
    //响应一个对象
    private void responseGraduateProject(int id, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        GraduateProjectStatus graduateProjectStatus = GraduateProjectStatusService.getInstance().find(id);
        String graduateProject_json = JSON.toJSONString(graduateProjectStatus);
        //响应json到前端
        response.getWriter().println(graduateProject_json);
    }
    //响应所有对象
    private void responseGraduateProjects(HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        //获得所有
        Collection<GraduateProjectStatus> graduateProjectSources = GraduateProjectStatusService.getInstance().findAll();
        String degrees_json = JSON.toJSONString(graduateProjectSources, SerializerFeature.DisableCircularReferenceDetect);
        response.getWriter().println(degrees_json);
    }
}
